<template>
  <div id="abnormal">
    <ul class="title">
      <li
        v-for="(item, index) in titleList"
        :key="index"
        :class="['title-item', { active: ckTitle == index }]"
        @click="selectTitle(index)"
      >
        {{ item }}
      </li>
    </ul>
    <div class="abnormal-num">
      异常游客机:<span>{{ abnormalSum }}</span>
    </div>
    <div class="query">
      <div class="tour-id">
        <span>游客机编号:</span>
        <el-input
          placeholder="输入游客机编号"
          v-model="tourId"
          clearable
          size="small"
          @clear="cancelId"
        >
        </el-input>
      </div>
      <div class="tour-name">
        <span>责任人姓名:</span>
        <el-input
          placeholder="请输入责任人姓名"
          v-model="tourName"
          clearable
          size="small"
          @clear="cancelName"
        >
        </el-input>

        <el-button type="primary" round size="small" @click="queryTour"
          >查询</el-button
        >
      </div>
    </div>
    <div class="table">
      <el-table :data="tableData" border style="width: 100%" size="small">
        <el-table-column prop="exceptionId" label="序号" width="140">
        </el-table-column>
        <el-table-column prop="num" label="游客机编号" width="240">
        </el-table-column>
        <el-table-column prop="exceptionTime" label="异常时间" width="240">
        </el-table-column>
        <el-table-column prop="exceptionPerson" label="责任人" width="240">
        </el-table-column>
        <el-table-column prop="exceptionStates" label="当前状态" width="240">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              v-show="
                scope.row.exceptionStates != '不可使用' &&
                scope.row.exceptionStates != '正常'
              "
              @click="editStates(scope.row)"
              >{{
                scope.row.exceptionStates == "丢失" ? "找回" : "修复"
              }}</el-button
            >

            <el-button
              type="text"
              size="small"
              @click="handleClick(scope.row, scope.index)"
              v-show="
                scope.row.exceptionStates != '正常' &&
                scope.row.exceptionStates != '不可使用'
              "
              >赔付</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :total="total" @send="send"></el-pagination>
    </div>
  </div>
</template>

<script>
// 引入分页组件
const Pagination = () => import("@/components/Pagination.vue");
import {
  getAbnormalList,
  getAbnormalTotal,
  editAbnormal,
} from "@/network/api/device";
export default {
  components: {
    "el-pagination": Pagination,
  },
  data() {
    return {
      titleList: ["正常", "异常"],
      ckTitle: 1,
      tourId: "",
      tourName: "",
      tableData: [],
      abnormalSum: null,
      current: 1,
      size: 5,
      total: null,
    };
  },
  created() {
    this.getTable(this.current, this.size, this.tourId, this.tourName);
    this.getTotal();
  },
  methods: {
    // 选择标题显示不同样式
    selectTitle(index) {
      this.ckTitle = index;
      if (index == 0) {
        this.$router.replace({ path: "/tourGuide/normal" });
      }
    },
    // table组件事件
    // 找回/修复
    async editStates(row) {
      let res = await editAbnormal(row.exceptionId, 1);
      if (res.data.code == 200) {
        this.$message({
          message: res.data.message,
          type: "success",
        });
        this.getTable(this.current, this.size, this.tourId, this.tourName);
      }
    },
    // 赔付
    async handleClick(row) {
      let res = await editAbnormal(row.exceptionId, 2);
      if (res.data.code == 200) {
        this.$message({
          message: res.data.message,
          type: "success",
        });
        this.getTable(this.current, this.size, this.tourId, this.tourName);
      }
    },
    // 获取异常游客机总数
    async getTotal() {
      let res = await getAbnormalTotal();
      console.log(res, 111);
      this.abnormalSum = res.data.data.abnormalCount;
    },

    //获取列表数据
    async getTable(current, size, id, name) {
      let res = await getAbnormalList(current, size, id, name);
      console.log(res, 1111);
      this.total = res.data.data.total;
      this.tableData = res.data.data.VisitorMachineAbnormalList.map((item) => {
        if (item.exceptionStates == 1) {
          item.exceptionStates = "正常";
        } else if (item.exceptionStates == 2) {
          item.exceptionStates = "不可使用";
        } else if (item.exceptionStates == 3) {
          item.exceptionStates = "丢失";
        } else {
          item.exceptionStates = "维修";
        }
        return {
          exceptionId: item.exceptionId,
          exceptionPerson: item.exceptionPerson,
          exceptionStates: item.exceptionStates,
          exceptionTime: item.exceptionTime,
          num: item.num,
        };
      });
    },
    send(args) {
      if (typeof args == "object") {
        this.current = args.data;
      } else {
        this.size = args;
      }
      this.getTable(this.current, this.size, this.tourId, this.tourName);
    },
    // 查询
    queryTour() {
      this.getTable(this.current, this.size, this.tourId, this.tourName);
    },

    cancelId() {
      this.tourId = "";
      this.current = 1;
      this.getTable(this.current, this.size, this.tourId, this.tourName);
    },
    cancelName() {
      this.tourName = "";
      this.current = 1;
      this.getTable(this.current, this.size, this.tourId, this.tourName);
    },
  },
};
</script>

<style  scoped lang="scss">
#abnormal {
  width: 100%;
  .title {
    display: flex;
    width: 200px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    border-radius: 4px;
    font-size: 14px;
    .title-item {
      width: 50%;
      height: 100%;
      color: #1890ff;
      border-radius: 4px;
      border: 1px solid #94ccff;
      cursor: pointer;
    }
  }
  .abnormal-num {
    margin: 10px 0;
    font-size: 14px;
    color: #505050;
    span {
      font-weight: 700;
      margin-left: 10px;
    }
  }
  .query {
    display: flex;
    width: 80%;
    padding: 0 20px;
    .tour-id,
    .tour-name {
      width: 36%;
      margin: 10px 0;
      font-size: 14px;
      color: #505050;
      display: inline-block;
      align-items: center;
      .el-input {
        width: 50%;
        margin: 0 10px;
      }
    }
  }
}
.el-pagination {
  text-align: center;
  margin-top: 10px;
}
.active {
  color: white !important;
  background-color: #1890ff;
}
.el-table .cell {
  padding-left: 0;
}
</style>